<?php

namespace App\Http\Controllers\Admin\Recruit;

use App\Http\Controllers\BaseController;
use App\Models\Recruit;
use App\Services\RecruitService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class RecruitController extends BaseController
{
    /**
     * 获取招工列表
     * @param Request $request
     * @return JsonResponse
     */
    public function list(Request $request): JsonResponse
    {
        $page      = $request->input('page', 1);
        $limit     = $request->input('limit', 1);
        $sortField = $request->input('sort', 'updated_at');
        $orderRule = $request->input('order', 'desc');

        try {
            $data = (new RecruitService())->getRecruitList($page, $limit, $sortField, $orderRule);
        } catch (\Exception $e) {
            return result(1, $e->getMessage());
        }

        return result(0, 'ok', $data);
    }

    /**
     * 编辑招工
     * @param Request $request
     * @return JsonResponse
     */
    public function edit(Request $request): JsonResponse
    {
        $rule        = [
            "recruit_id" => "required|integer|exists:recruit,id",
            "status"     => "required|integer"
        ];
        $input       = $request->input();
        $validateRet = Validator::make($input, $rule);
        if ($validateRet->fails()) {
            return result(1, $validateRet->errors()->first());
        }

        try {
            $data = Recruit::query()->where('id', $input['recruit_id'])->update(['status' => $input['status']]);
        } catch (\Exception $e) {
            return result(1, $e->getMessage());
        }
        return result(0, 'ok');
    }
}
